package hot100.logos41To50;

import hot100.datastruct.TreeNode;

/**
 * @author logos
 * date 2025/7/14 11:56
 * @version v1.0
 * @Package hot100.logos41To50
 */
public class logos42 {

    /**
     * 42. 将有序数组转换为平衡二叉搜索树
     * https://leetcode.cn/problems/convert-sorted-array-to-binary-search-tree/description/?envType=study-plan-v2&envId=top-100-liked
     */
    class Solution {
        public TreeNode sortedArrayToBST(int[] nums) {
            return solve(nums, 0, nums.length - 1);
        }

        public TreeNode solve(int[] nums, int l, int r) {
            if (l > r) {
                return null;
            }
            int mid = l + (r - l) / 2;
            TreeNode root = new TreeNode(nums[mid]);
            root.left = solve(nums, l, mid - 1);
            root.right = solve(nums, mid + 1, r);
            return root;
        }
    }
}
